package com.onlyxiahui.app.base;

import com.onlyxiahui.app.base.component.AbstractBox;
import com.onlyxiahui.app.base.component.AbstractController;
import com.onlyxiahui.app.base.component.AbstractManager;
import com.onlyxiahui.app.base.component.AbstractSender;
import com.onlyxiahui.app.base.component.AbstractService;
import com.onlyxiahui.app.base.task.ExecuteTask;
import com.onlyxiahui.app.base.task.QueueTaskThread;
import com.onlyxiahui.app.base.view.View;

/**
 * 通用程序的上下文，用于管理整个程序对象管理<br>
 * 和模块之间的桥梁
 * date 2017-09-20 3:22:56
 * @author XiaHui
 */
public class AppContext {

	QueueTaskThread queueTaskThread = new QueueTaskThread();
	ObjectFactory objectFactory = new ObjectFactory(this);
	

	/**
	 * 放入已经实例化的对象
	 * 
	 * @author XiaHui
	 * @param defineClass
	 * @param object
	 */
	public <T> void put(Class<T> defineClass, T object) {
		objectFactory.put(defineClass, object);
	}

	/**
	 * 注册接口或者父类所对应的实现子类，当根据类型获取对象时就会获取此实现类的实例
	 * 
	 * @author XiaHui
	 * @param defineClass：父类或者接口
	 * @param instanceClass：实现子类
	 */
	public <T> void register(Class<T> defineClass, Class<? extends T> instanceClass) {
		objectFactory.register(defineClass, instanceClass);
	}

	public <T> T getObject(Class<T> classType) {
		return getObject(classType, false, false);
	}

	public <T> T getObject(Class<T> classType, boolean createNew) {
		return getObject(classType, createNew, false);
	}

	public <T> T getObject(Class<T> classType, boolean createNew, boolean cover) {
		T t = objectFactory.getObject(classType, createNew, cover);
		return t;
	}

	public <T extends View> T getSingleView(Class<T> classType) {
		return this.getObject(classType);
	}

	public <T extends AbstractService> T getService(Class<T> classType) {
		return this.getObject(classType);
	}

	public <T extends AbstractManager> T getManager(Class<T> classType) {
		return this.getObject(classType);
	}

	public <T extends AbstractSender> T getSender(Class<T> classType) {
		return this.getObject(classType);
	}

	public <T extends AbstractController> T getController(Class<T> classType) {
		return this.getObject(classType);
	}

	public <T extends AbstractBox> T getBox(Class<T> classType) {
		return this.getObject(classType);
	}

	public <T extends AbstractComponent> T getModule(Class<T> classType) {
		return this.getObject(classType);
	}

	public void add(ExecuteTask task) {
		queueTaskThread.add(task);
	}
}
